package com.qf.mybatisplus.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.qf.mybatisplus.bean.entity.User;
import com.qf.mybatisplus.bean.vo.RegistVo;
import com.qf.mybatisplus.resultBean.JsonResult;
import com.qf.mybatisplus.service.UserService;
import com.qf.mybatisplus.utils.RedisUtils;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author 12136
 * @version 1.0
 * @description: TODO
 * @date 2022/2/17 14:17
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    private static final Logger log = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtils redisUtils;
    /**
     * 用户登录接口
     *
     * @param user    user
     * @param request request
     * @return string
     */
    @RequestMapping("/doLogin")
    @ApiOperation(value = "登录接口")
    public JsonResult login(User user, HttpServletRequest request) {

        try {
            User user1 = (User) request.getSession().getAttribute("user");
            System.err.println("登录成功success");
            System.err.println("user1=" + user1);


            return JsonResult.loginOk(user1);
        } catch (Exception e) {
            e.printStackTrace();
            request.getSession().setAttribute("user", user);
            request.setAttribute("error", "用户名或密码错误！");

            return JsonResult.loginFailed(user);
        }
    }

    @GetMapping("/getCode")
    @CrossOrigin
    public JsonResult getCode(){

        /**、
         * 1、创建一个验证码：
         *  验证码 captcha['kæptʃə]
         *  line:线条
         *  参数说明
         *  width:宽度
         *  height：高度
         *  codeCount:字符数量
         *  lineCount:干扰线数量
         */
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 40, 4, 4);
        //2.获得生成的验证码的真实码值
        String code = lineCaptcha.getCode();
        System.err.println("验证码:"+code);
        //3.将验证码图片输出到D盘根目录下
        String uuid = UUID.randomUUID().toString().replace("-", "");
        try {
            lineCaptcha.write(new FileOutputStream("/Users/zmkj/ideaProjects/my-project/static/code/"+uuid+".jpg"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String ipPath = "http://192.168.0.66:8091/MOKO-IMG/mybatisPlusFile/"+uuid+".jpg";
        return JsonResult.resultOk(ipPath);
    }


    @PostMapping("/doRegist")
    public JsonResult doRegist(@RequestBody RegistVo registVo) {
        log.info("注册前端传入参数：{}",registVo.toString());
        return userService.toRegist(registVo);
    }

    @GetMapping("/getMesCode")
    public JsonResult getMesCode() {
        redisUtils.set("mesCode","123456",600L, TimeUnit.SECONDS);
        // 走短信服务

        return JsonResult.success();
    }


    public static void main(String[] args) {

    }
}
